package com.xul.service;


import com.github.pagehelper.PageHelper;
import com.xul.dao.StudentDao;
import com.xul.entity.Student;
import com.xul.utils.BeanMapUtils;
import com.xul.utils.MD5Utils;
import com.xul.utils.MapParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xul
 * @create 2021-06-06 11:53
 */
@Service
public class StudentService {

    @Autowired
    private StudentDao studentDao;

    public int create(Student pi) {
        pi.setStuPwd(MD5Utils.getMD5(pi.getStuPwd()));
        return studentDao.create(pi);
    }

    public int delete(Integer id) {
        return studentDao.delete(MapParameter.getInstance().addId(id).getMap());
    }

    public int delete(String ids) {
        String[] arr = ids.split(",");
        int flag = 0;
        for (String s : arr) {
            flag = studentDao.delete(MapParameter.getInstance().addId(Integer.parseInt(s)).getMap());
        }
        return flag;
    }

    public int update(Student student) {

        Map<String, Object> map = MapParameter.getInstance().add(BeanMapUtils.beanToMapForUpdate(student)).addId(student.getId()).getMap();
        return studentDao.update(map);
    }

    public List<Student> query(Student student) {
        if (student != null && student.getPage() != null) {
            PageHelper.startPage(student.getPage(), student.getLimit());
        }
        return studentDao.query(BeanMapUtils.beanToMap(student));
    }

    public Student detail(Integer id) {
        return studentDao.detail(MapParameter.getInstance().addId(id).getMap());
    }

    public int count(Student student) {
        return studentDao.count(BeanMapUtils.beanToMap(student));
    }

    public Student login(String userName, String password) {
        Map<String, Object> map = MapParameter.getInstance().add("stuNo", userName).add("stuPwd", password).getMap();
        return studentDao.detail(map);
    }

    public List<HashMap> querySelectStudent(Integer courseId, Integer sectionId) {
        Map<String, Object> map = MapParameter.getInstance().add("courseId", courseId).add("sectionId", sectionId).getMap();
        return studentDao.querySelectStudent(map);
    }

    public List<Student> queryStudentByTeacher(Integer teacherId, Integer clazzId, Integer subjectId) {
        Map<String, Object> map = MapParameter.getInstance()
                .add("teacherId", teacherId)
                .add("clazzId", clazzId)
                .add("subjectId", subjectId)
                .getMap();
        return studentDao.queryStudentByTeacher(map);

    }
}
